<?php
/* 
* @Author: Jerry-chen
* @Date:   2014-07-16 02:46:52
* @Last Modified time: 2014-07-28 11:10:42
* @Last Modified by: Jerry-chen<uranusbill@163.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class AnswerControl extends CommonControl
{
	/**
	 * [index 问题列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 查出问题相关信息 
		$askData = K('Ask')->get_all();
		//查出商品的名字
		foreach ($askData as $k => $v) {
			$result = K('Goods')->get_one(array('goodsid'=>$v['goodsid']),array('goodsname'));
			$askData[$k]['goodsname'] = $result['goodsname'];
		}
		// p($askData);die;
		// p($askData);
		// // 是否回复
		foreach ($askData as $key => $value){
			$result = K('Answer')->get_one(array('askid'=>$value['askid']));
			if($result)
			{
				// 如果可以查到证明有回答过
				$askData[$key]['is_answer'] = '1';
			}else{
				// 如果无法查到证明没有回答过，是NULL
				$askData[$key]['is_answer'] = '0';
			}
			// p($result);die;
		}
		// p($askData);die;
		// p($askData);
		//分配数据
		$this->assign('askData',$askData);
		// die;
		$this->display('index.html');
	}
	/**
	 * [answer 回复]
	 * @return [type] [description]
	 */
	public function answer()
	{
		// 查出当前问题的ID
		$askData = K('ask')->get_one(array('askid'=>Q('get.askid')));
		//获得商品名
		$goodsname =K('Goods')->get_one(array('goodsid'=> $askData['goodsid']),array('goodsname'));
		$askData['goodsname']=$goodsname['goodsname'];
		// p($askData);die;
		// 分配数据
		if(IS_POST)
			{
				// P(Q('post.'));
				$answer = array(
					'goodsid'=>Q('post.goodsid'),
					'adminid'=>session('aid'),
					'adminname'=>session('username'),
					'anscontent'=>Q('post.anscontent'),
					'askid'=>Q('post.askid'),
					);
				// p($answer);die;
				$result = K('Answer')->add_($answer);
				if($result){
				$this->success('回复成功','index');
			}else{
				$this->error('回复失败','');
			}
		}
		$this->assign('askData',$askData);
		$this->display('edit.html');
	}
	/**
	 * [info 查看详情]
	 * @return [type] [description]
	 */
	public function info()
	{
		//查出当前ID的提问
		$askData = K('ask')->get_one(array('askid'=>Q('get.askid')));
		//获得商品名
		$goodsname =K('Goods')->get_one(array('goodsid'=> $askData['goodsid']),array('goodsname'));
		$askData['goodsname']=$goodsname['goodsname'];
		$this->assign('askData',$askData);
		// p($askData);die;
		// 通过提问ID，查出相关的答案
		$answerData = K('Answer')->get_all(array('askid'=>$askData['askid']));
		
		if(!$answerData)
		{
			$answerData=0;
			$this->assign('answerData',$answerData);
		}else{

			$this->assign('answerData',$answerData);
		}
		// p($answerData);die;
		$this->display();
	}
	/**
	 * [ajax_is_show 是否显示]
	 * @return [type] [description]
	 */
	public function ajax_is_show()
	{
		if(Q('post.is_show')==0){
			K('Ask')->edit(array('askid'=>Q('post.askid')),array('is_show'=>1));
			echo 1;die;
		}
		if(Q('post.is_show')==1){
			K('Ask')->edit(array('askid'=>Q('post.askid')),array('is_show'=>0));
			echo 0; die;
		}
	}
}